package com.api.weavernorth.action.sap;

import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;

/**
 * 刷新区域经理评分数据
 */
@Path("/weavernorth/action/sap/refresh")
public class RefreshRate extends HttpServlet {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(RefreshRate.class.getName());

    @GET
    @Path("/refreshRate")
    @Produces(MediaType.TEXT_PLAIN)
    public String refreshRate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String strMess = "";
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsSelect = new RecordSet();
        RecordSet rsJs = new RecordSet();
        RecordSet rsUp = new RecordSet();


        String strSql = "select xm,pfqj ,id,lsyypf,fgs from uf_qyjlpfhzb";
        log.info("查询区域经理评分汇总表:"+strSql);
        rs.executeQuery(strSql);
        while (rs.next()) {
            //区域经理
            String strQyjl = Util.null2String(rs.getString("xm"));
            //评分区间
            String strPfqj = Util.null2String(rs.getString("pfqj"));
            //区经汇总表的id
            String strId = Util.null2String(rs.getString("id"));
            //分公司
            String strFgs=Util.null2String(rs.getString("fgs"));
            //零售运营分数
            Double 	douLsyypf=Util.getDoubleValue(rs.getString("lsyypf"));
            //CRM评分
            Double douCrm = 0.0;
            //陈列管理
            Double douManger = 0.0;
            //服务标准
            Double douFwbzpf = 0.0;
            //总得分
            Double douSum = 0.0;
            //奖金基数
            Double douJs=0.0;
            //根据区域经理的xm查询店长的数据
            String strPfSql = "select udz.crm,udz.fwbzpf,udz.clglpf,udz.fs from uf_dpxx ud,uf_dzpfhzb udz where ud.dc=udz.xm and ud.qyjl=? and udz.pfqj=?";
            log.info("查询店长的分数:" + strPfSql + ",strQyjl:" + strQyjl + ",strPfqj:" + strPfqj);
            rsSelect.executeQuery(strPfSql, strQyjl, strPfqj);
            //总条数
            int intCount = rsSelect.getCounts();
            log.info("总条数:" + intCount);
            while (rsSelect.next()) {
                douCrm = douCrm + Util.getDoubleValue(rsSelect.getString("crm"));
                douFwbzpf = douFwbzpf + Util.getDoubleValue(rsSelect.getString("fwbzpf"));
                douManger = douManger + Util.getDoubleValue(rsSelect.getString("clglpf"));


            }
            if (intCount > 0) {
                BigDecimal bigCrm = new BigDecimal(douCrm / intCount);
                Double douAverCrm = bigCrm.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                BigDecimal bigFwbzpf = new BigDecimal(douFwbzpf / intCount);
                Double douAverFwbzpf = bigFwbzpf.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                BigDecimal bigManger = new BigDecimal(douManger / intCount);
                Double douAverManger= bigManger.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                //更新区域经理评分
               // log.info("零售运营分数:"+douLsyypf+",douAverCrm:"+douAverCrm+",douAverFwbzpf:"+douAverFwbzpf+",douAverManger:"+douAverManger);
                douSum= douLsyypf+douAverCrm+douAverFwbzpf+douAverManger;

                //查询区域经理奖金基数
                String strJsSql="select qyjljs  from uf_jjjspzb where fgs=?";
                rsJs.executeQuery(strJsSql,strFgs);
                if(rsJs.next()){
                    douJs=Util.getDoubleValue(rsJs.getString("qyjljs"));
                }
                BigDecimal bigJs = new BigDecimal(douSum *douJs/100);
                Double doujjJs= bigJs.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();


                log.info("douSum:"+douSum);
                String strUpSql="update  uf_qyjlpfhzb set crmpf=?,fwbzpf=?,clglpf=?,zdf=?,jjje=? where id=?";
                log.info("更新区域经理评分:"+strUpSql+",douAverCrm:"+douAverCrm+"," +
                        "douAverFwbzpf:"+douAverFwbzpf+",douAverManger:"+douAverManger+",douSum:"+douSum+",strId:"+strId);
                rsUp.executeQuery(strUpSql,douAverCrm,douAverFwbzpf,douAverManger,douSum,doujjJs,strId);

            }


        }

        return strMess;
    }

}